package com.xuxin.baby.activity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.blankj.utilcode.util.ActivityUtils;
import com.blankj.utilcode.util.GsonUtils;
import com.lxj.xpopup.XPopup;
import com.xuxin.baby.R;
import com.xuxin.baby.adapter.OrderCommitAdapter;
import com.xuxin.baby.base.BaseActivity;
import com.xuxin.baby.base.BaseConstant;
import com.xuxin.baby.bean.BaseBean;
import com.xuxin.baby.bean.OrderCommitBean;
import com.xuxin.baby.bean.UpdateOrderBean;
import com.xuxin.baby.contract.OrderCommitContract;
import com.xuxin.baby.popup.ConfirmPopup;
import com.xuxin.baby.presenter.OrderCommitPresenter;
import com.xuxin.baby.utils.ACache;
import com.xuxin.baby.utils.RecycleUtils;

import butterknife.BindView;
import butterknife.OnClick;
import okhttp3.MediaType;
import okhttp3.RequestBody;

/**
 * Created by xu_xin
 * Created on 2020/11/17 0017
 * 提交订单
 */
public class OrderCommitActivity extends BaseActivity implements OrderCommitContract.View {
    private final OrderCommitContract.Presenter presenter = new OrderCommitPresenter(this);
    @BindView(R.id.title_left)
    ImageView title_left;
    @BindView(R.id.title_right)
    ImageView title_right;
    @BindView(R.id.title_name)
    TextView title_name;
    @BindView(R.id.order_commit_recycle)
    RecyclerView order_commit_recycle;
    @BindView(R.id.order_commit_price)
    TextView order_commit_price;

    private OrderCommitAdapter myAdapter;
    private String memberId;//用户id
    private String tenantId;//租户id
    private String token;//授权token
    private RequestBody body;

    @Override
    public void setContentView() {
        setContentView(R.layout.activity_order_commit);
    }

    @Override
    public void initView() {
        title_name.setText("确认订单");
        RecycleUtils.init(order_commit_recycle);
        myAdapter = new OrderCommitAdapter();
        order_commit_recycle.setAdapter(myAdapter);
    }

    @Override
    public void initData() {
        ACache aCache = ACache.get(this);
        memberId = aCache.getAsString(BaseConstant.memberId);
        tenantId = aCache.getAsString(BaseConstant.tenantId);
        token = aCache.getAsString(BaseConstant.token);
        String order = getIntent().getStringExtra("order");
        OrderCommitBean orderCommitBean = GsonUtils.fromJson(order, OrderCommitBean.class);
        myAdapter.setList(orderCommitBean.getDetails());
        order_commit_price.setText(orderCommitBean.getSummary());
        body = RequestBody.create(order.toString(), MediaType.parse("application/json"));
    }

    @Override
    public void onComplete() {
        dismiss();
    }

    @Override
    @OnClick({R.id.title_left, R.id.order_commit})
    public void onClick(View view) {
        int id = view.getId();
        if (id == R.id.title_left) {
            finish();
        } else if (id == R.id.order_commit) {
            new XPopup.Builder(context)
                    .dismissOnTouchOutside(true)
                    .dismissOnBackPressed(true)
                    .asCustom(new ConfirmPopup(context, "确定要支付吗？", new ConfirmPopup.OnConfirmListener() {
                        @Override
                        public void onConfirm() {
                            if (null != body) {
                                try {
                                    popup.setTitle("正在支付中...").show();
                                    handler.sendEmptyMessage(0);
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    dismiss();
                                }
                            }
                        }
                    })).show();
        }
    }

    private final Handler handler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case 0:
                    presenter.updateOrder(tenantId, token, memberId, body);
                    break;
                case 1:
                    presenter.updateOrderStatus(tenantId, token, updateOrderBean.getResult().getOrderNo(), "已完成","bean","trade");
                    break;
            }
            return false;
        }
    });

    private  UpdateOrderBean updateOrderBean;
    @Override
    public void onSuccess(UpdateOrderBean updateOrderBean) {
        this.updateOrderBean = updateOrderBean;
        handler.sendEmptyMessage(1);
    }

    @Override
    public void onSuccess(BaseBean baseBean) {
        setResult(200);
        ActivityUtils.startActivity(UserOrderActivity.class);
        finish();
    }
}